<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);

class c_ajax_class extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action["guest"] = array();
		$rule_action["user"] = array();
		$rule_action["actions"] = array();
		
		return $rule_action;
	}

	public function fetch_question_category_action()
	{
		echo $this->model('system')->build_category_json('question', 0, $question_info['category_id']);
		exit;
	}

	public function question_attach_upload_action()
	{
		if (get_setting('upload_enable') != 'Y')
		{
			die;
		}
		
		define('IROOT_PATH', get_setting('upload_dir') . '/questions/');
		define('ALLOW_FILE_FIXS', '*');
		
		$this->model('upload')->set_upload_dir('');
		
		$date = date('Ymd');
		
		if (isset($_GET['qqfile']))
		{
			$imagepath = $this->model('upload')->xhr_upload_file(file_get_contents('php://input'), $_GET['qqfile'], $date);
			
			$file_name = $_GET['qqfile'];
		}
		else if (isset($_FILES['qqfile']))
		{
			$imagepath = $this->model('upload')->upload_file($_FILES['qqfile'], $date);
			
			$file_name = $_FILES['qqfile']['name'];
		}
		else
		{
			return false;
		}
		
		if (! $imagepath)
		{
			return false;
		}
		
		$fileinfo = pathinfo($imagepath);
		
		$file_type = $this->model('upload')->get_filetype($imagepath);
		$file_size = $this->model('upload')->get_filesize();
		$sfile_type = ltrim($file_type, ".");
		$img_type_arr = explode(",", 'jpg,png,gif,jpeg');
		
		// 则生成缩略图
		if (in_array($sfile_type, $img_type_arr))
		{			
			foreach(AWS_APP::config()->get('image')->attachment_thumbnail AS $key => $val)
			{
				$thumb_file[$key] = $this->model('image')->make_thumb(IROOT_PATH . $imagepath, $val['w'], $val['h'], IROOT_PATH . $fileinfo['dirname'] . '/', $val['w'].'x'.$val['h'].'_' . $fileinfo['basename'], true);
					
			}
			
			$min_thumb = $thumb_file['square'];
			
			if ($min_thumb)
			{
				$thumb = get_setting('upload_url') . '/questions/'.$date.'/' . $min_thumb;
			}
		}
		
		$attach_id = $this->model('publish')->add_question_attach($file_name, $_GET['attach_access_key'], time(), $fileinfo['basename'], $thumb);
		
		$output = array(
			'success' => true
		);
		
		if ($thumb)
		{
			$output['thumb'] = $thumb;
		}
		else
		{
			$output['class_name'] = $this->model('publish')->get_file_class($sfile_type);
		}
		
		$output['delete_url'] = get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'publish/ajax/remove_question_attach/attach_id-' . base64_encode(H::encode_hash(array(
			'attach_id' => $attach_id, 
			'access_key' => $_GET['attach_access_key']
		)));
		
		echo htmlspecialchars(json_encode($output), ENT_NOQUOTES);
	}

	public function remove_question_attach_action()
	{		
		if(!$this->user_info['permission']['edit_question'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限进行操作'));
			exit();
		}
		
		$attach_info = H::decode_hash(base64_decode($_GET['attach_id']));
		
		$this->model('publish')->remove_question_attach($attach_info['attach_id'], $attach_info['access_key']);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '附件删除成功'));
	}



	function modify_question_action()
	{		
		$question_id = intval($_POST['question_id']);
		
		$question_info = $this->model('question')->get_question_info_by_id($question_id);
		
		if (empty($question_info))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '问题不存在'));
		}
		
		if (!$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{			
			if ($question_info['published_uid'] != $this->user_id OR !$this->user_info['permission']['edit_question'])
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限编辑这个问题'));
			}
			
			if ((time() - $question_info['add_time']) > intval(get_setting('question_edit_time')) * 60)
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '对不起, 发布问题超过 ' . get_setting('question_edit_time') . ' 分钟以后无法修改!'));
			}
		}
		
		$question_content = $_POST['question_content'];
		$question_detail = $_POST['question_detail'];
		
		if (!$_POST['category_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, - 1, '请选择分类'));
		}

		if (cjk_strlen($question_content) < 5)
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '问题标题字数不得少于 5 个，请返回修改'));
		}

		if ((get_setting('question_title_limit') > 0) && (cjk_strlen($question_content) > get_setting('question_title_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '问题标题字数不得大于 ' . get_setting('question_title_limit') . ' 个，请返回修改。'));
		}
		
		if (!$this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($question_detail))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "你所在的用户组不允许发布站外链接"));
		}
		
		if (human_valid('question_valid_hour') AND !core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', "请填写正确的验证码"));
		}
		
		// !注: 来路检测后面不能再放报错提示
		if (!valid_post_hash($_POST['post_hash']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '表单来路不正确或内容已提交, 请刷新页面重试'));
		}
		
		core_captcha::clear();

		//分词处理
		$this->model('question')->question_phpanalysis_keyword($question_id,$question_content);
	
		$this->model('question')->update_question($question_id, $question_content, $question_detail);
		
		$this->model('question')->update_question_category($question_id, intval($_POST['category_id']));
		
		if (intval($_POST['category_id']) != $question_info['category_id'])
		{
			$category_info = $this->model('system')->get_category_info(intval($_POST['category_id']));
			ACTION_LOG::save_action($this->user_id, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::MOD_QUESTION_CATEGORY, $category_info['title'], $category_info['id']);
		}
		
		ZCACHE::cleanGroup("question_detail_" . $question_id);
		
		if ($_POST['attach_access_key'])
		{
			if($this->model('publish')->update_question_attach($question_id, $_POST['attach_access_key']))
			{
				ACTION_LOG::save_action($this->user_id, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::MOD_QUESTION_ATTACH);
			}
		}
		
		$this->model('associate_index')->update_update_time($question_id, 1);
		$this->model('associate_index')->update_update_time($question_id, 3);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'url' => get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'question/' . $question_id
		), 1, "修改问题成功!"));
	
	}
	
	public function publish_question_action()
	{
		if (!$this->user_info['permission']['publish_question'] AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '你没有权限发布问题'));
		}
		
		$question_content = $_POST['question_content'];
		$question_detail = $_POST['question_detail'];
		
		$topic_id = intval($_POST['topic_id']);
		
		if (empty($question_content))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), - 1, '请输入问题'));
		}
		
		if (!$_POST['category_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), - 1, '请选择问题分类'));
		}

		if (cjk_strlen($question_content) < 5)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), '-1', '问题标题字数不得少于 5 个，请返回修改。'));
		}

		if ((get_setting('question_title_limit') > 0) && (cjk_strlen($question_content) > get_setting('question_title_limit')))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), '-1', '问题标题字数不得大于 ' . get_setting('question_title_limit') . ' 个，请返回修改。'));
		}
		
		if (!$this->user_info['permission']['publish_url'] && FORMAT::outside_url_exists($question_detail))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), '-1', "你所在的用户组不允许发布站外链接"));
		}
		
		if (human_valid('question_valid_hour') AND !core_captcha::validate($_POST['seccode_verify'], false))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), '-1', "请填写正确的验证码"));
		}
		
		// !注: 来路检测后面不能再放报错提示
		if (!valid_post_hash($_POST['post_hash']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'tips_id' => 'quick_publish_message'
			), '-1', '表单来路不正确或内容已提交, 请刷新页面重试'));
		}
		
		core_captcha::clear();
		
		if ($question_id = $this->model('question')->save_question($question_content, $question_detail, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
		{
			set_human_valid('question_valid_hour');
			
			//分词处理			
			$this->model('question')->question_phpanalysis_keyword($question_id,$question_content);
			
			$this->model('question')->update_question_category($question_id, intval($_POST['category_id']));
			
			if ($topic_id > 0)
			{
				$topic = $this->model('topic')->get_topic($topic_id);
				
				if (! empty($topic))
				{
					$this->model('question_topic')->save_link($topic_id, $question_id);
				}
			}
			
			$this->model('question')->update_question_state($question_id, $this->user_id, ACTION_LOG::ADD_REQUESTION_FOCUS);
			
			if (is_array($_POST['topics']))
			{
				foreach ($_POST['topics'] as $key => $topic_title)
				{
					$topic_id = $this->model('topic')->save_topic($question_id, $topic_title, $this->user_id);
					
					$this->model('question_topic')->save_link($topic_id, $question_id);
				}
			}
			
			if ($_POST['attach_access_key'])
			{
				$this->model('publish')->update_question_attach($question_id, $_POST['attach_access_key']);
			}
			
			if ($_POST['ask_user_id'])
			{
				$this->model('question')->add_invite($question_id, $this->user_id, $_POST['ask_user_id']);
				
				$this->model('notify')->send($this->user_id, $_POST['ask_user_id'], notify_class::TYPE_INVITE_QUESTION, notify_class::CATEGORY_QUESTION, $question_id, array(
					'from_uid' => $this->user_id,
					'question_id' => $question_id,
				));
			}
			
			//自动关注该问题
			$this->model('question')->add_focus_question($question_id, false);
			
			//索引
			$this->model("associate_index")->add_associate_index($this->user_id, $question_id, 3);
			
			//记录日志
			ACTION_LOG::save_action($this->user_id, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_QUESTION, htmlspecialchars(trim($question_content)), htmlspecialchars(trim($question_detail)));
			
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_setting('base_url') . '/' . G_INDEX_SCRIPT . 'question/' . $question_id
			), 1, '问题发布成功'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '问题发布失败, 请联系管理员'));
		}
	}
}